<template>
  <div class="app-container" v-loading="loading.getPageList">
    <div>
      <!-- <el-button type="primary" size="small" @click="create">新增</el-button> -->
      <el-button type="primary" size="small" @click="getPageList">刷新</el-button>
    </div>
    <br />

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="index" label="序号" width="60"> </el-table-column>
       <el-table-column prop="name" label="标题"> </el-table-column>
      <!-- <el-table-column prop="formDesignTitle" label="表单" width="200">
      </el-table-column> -->
      <el-table-column prop="workflowName" label="流程名称" width="250">
        <template  slot-scope="scope">
          <span>{{scope.row.workflowId.workflowName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批状态" width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.workTaskStatus == 0">待发起</span>
          <span v-if="scope.row.workTaskStatus == 1">审批中</span>
          <span v-if="scope.row.workTaskStatus == 2">已审批</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="creationTime"
        :formatter="dateFormat"
        label="创建时间"
        width="160"
      >
      </el-table-column>
      <el-table-column
        prop="modifiedTime"
        :formatter="dateFormat"
        label="更新时间"
        width="160"
      >
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.workTaskStatus != 0"
            @click="$refs.viewForm.show(scope.row.entityKeyValue)"
            type="text"
            size="small"
            >查看</el-button
          >
          <el-button
            v-if="scope.row.workTaskStatus == 0"
            @click="$refs.editForm.show(scope.row.entityKeyValue)"
            type="text"
            size="small"
            >编辑</el-button
          >

          <el-button
            v-if="scope.row.workTaskStatus == 0"
            @click="remove(scope.row.id)"
            type="text"
            size="small"
            >删除</el-button
          >
          <el-button
            v-if="scope.row.workTaskStatus == 0"
            @click="startwork(scope.row)"
            type="text"
            size="small"
            >发起审批</el-button
          >
            <el-button
            @click="$refs.workflowDesignForm.show({id:scope.row.workflowId.id,version:scope.row.workflowId.versionId})"
            type="text"
            size="small"
            >查看流程</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <br />
    <el-pagination
      background
      layout="sizes,prev, pager, next"
      :total="page.total"
      :page-size="page.maxResultCount"
      :current-page="page.currentPage"
      @current-change="(current) => paginationChange(null, current)"
      @size-change="(size) => paginationChange(size)"
    >
    </el-pagination>

    <viewForm ref="viewForm" ></viewForm>
    <editForm ref="editForm" @ok-click="getPageList"></editForm>
    <createForm ref="createForm" @ok-click="getPageList"></createForm>
    
    <workflowDesignForm ref="workflowDesignForm"></workflowDesignForm>
  </div>
</template>

<script>
import createForm from "./createForm";
import TableIndex from "@/layout/mixin/TableIndex";
import editForm from "./editForm";
import viewForm from './viewForm'

import workflowDesignForm from '@/views/workflows/workflow/viewWorkflowForm.vue'
export default {
  components: { createForm, editForm ,viewForm,workflowDesignForm},
  mixins: [TableIndex],
  data() {
    return {
      moduleName: "workflow",
      selections: [],
    };
  },
  mounted() {
  },
  methods: {
    getPageList() {
      this.loading.getPageList = true
      this.page.skipCount =
        (this.page.currentPage - 1) * this.page.maxResultCount;
      this.$store.dispatch(this.moduleName + "/getWorkTasksOfCreator", this.page).then((res) => {
        this.tableData = res.items;
        this.page.total = res.total;
        this.loading.getPageList = false
      });
    },
    getDesignName(designId) {
      var designs = this.selections.filter((s) => s.id == designId);
      return designs && designs.length > 0 ? designs[0].title : "";
    },
    startwork(row) {
      this.loading.getPageList=true;
      return this.$store
        .dispatch("dynamicFormInstance/startWorkTask", {workTaskId:row.id,formInstanceId:row.entityKeyValue})
        .then((res) => {
          setTimeout(this.getPageList, 500);
          this.$message({
          message: '发起成功，稍后刷新更新状态！',
          type: 'success'
        });
        }).catch(e=>{
          this.loading.getPageList = false;
        });
    },
    remove(id) {
      this.$confirm("确认删除该记录？, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          this.$store.dispatch(this.moduleName + "/deleteWorkTasks", [id]).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getPageList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style></style>
